package com.guoxue.util;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.easemob.server.api.IMUserAPI;
import com.easemob.server.comm.ClientContext;
import com.easemob.server.comm.EasemobRestAPIFactory;
import com.easemob.server.comm.body.IMUserBody;
import com.easemob.server.comm.body.UserNamesBody;
import com.easemob.server.comm.wrapper.BodyWrapper;
import com.easemob.server.comm.wrapper.ResponseWrapper;
/**
 * 环信聊天工具类
 * @author young
 *
 */
public class ChatUtils {
	
	private static EasemobRestAPIFactory factory;
	
	private static IMUserAPI userAPI;
	
	static{
		factory = ClientContext.getInstance().init(ClientContext.INIT_FROM_PROPERTIES).getAPIFactory();
		userAPI = (IMUserAPI)factory.newInstance(EasemobRestAPIFactory.USER_CLASS);
	}
	/**
	 * 创建单个聊天用户
	 * @param UserId
	 * 		  用户id
	 * @param password
	 *  	 用户密码
	 * @param nickName
	 * 	          用户昵称
	 * @return
	 */
	public static Boolean creatSinglUser(String UserId,String password, String nickName){
		BodyWrapper userBody = new IMUserBody(UserId, password, nickName);
		ResponseWrapper re = (ResponseWrapper) userAPI.createNewIMUserSingle(userBody);
		if (re.getResponseStatus()==200){
			return true;
		} 
		return false;
	}
	/**
	 * 删除用户
	 * @param userId
	 * 			用户id
	 * @return
	 */
	public static Boolean delUser(String userId){
		ResponseWrapper re = (ResponseWrapper) userAPI.deleteIMUserByUserName(userId);
		if (re.getResponseStatus()==200){
			return true;
		}
		return false;
	}
	
	/**
	 * 添加好友
	 * @param userId
	 * 			用户id
	 * @param friendId
	 * 			朋友id
	 * @return
	 */
	public static Boolean addFriendSingle(String userId, String friendId){
		ResponseWrapper re = (ResponseWrapper) userAPI.addFriendSingle(userId, friendId);
		if (re.getResponseStatus()==200){
			return true;
		}
		return false;
	}
	/**
	 * 删除好友
	 * @param userId
	 * @param friendId
	 * @return
	 */
	public static Boolean delFriendSingle(String userId, String friendId){
		ResponseWrapper re = (ResponseWrapper) userAPI.deleteFriendSingle(userId, friendId);
		if (re.getResponseStatus()==200){
			return true;
		}
		return false;
	}
	/**
	 * 加入黑名单
	 * @param uid
	 * 			用户id
	 * @param blacker
	 * 			被拉黑者id String[]
	 * @return
	 */
	public static Boolean addBlackList(String uid,String[] str){
		BodyWrapper userBody = new UserNamesBody(str);
		ResponseWrapper rw = (ResponseWrapper)userAPI.addToBlackList(uid, userBody);
		if (rw.getResponseStatus()==200){
			return true;
		}
		return false;
	}
}
